import 'package:dailoz/entity/task_time_entity.dart';
import 'package:dailoz/generated/l10n.dart';
import 'package:dailoz/service/task_page_service/taskPage_service.dart';
import 'package:dailoz/size_config.dart';
import 'package:dailoz/widgets/progress_widget.dart';
import 'package:flutter/material.dart';
import 'package:table_calendar/table_calendar.dart';

class TaskPage extends StatefulWidget {
  const TaskPage({Key? key}) : super(key: key);

  static String id = "TaskPageId";

  @override
  State<TaskPage> createState() => _TaskPageState();
}

class _TaskPageState extends State<TaskPage> {
  List<Widget> taskList = [];

  CalendarFormat _calendarFormat = CalendarFormat.week;

  DateTime? _selectedDay = DateTime.now();
  DateTime? _focusedDay = DateTime.now();
  final DateTime _firstDay = DateTime.now();

  bool isLoading = false;

  Map<String, dynamic> map = {};
  Map<String, dynamic> totalTimeMap = {};

  int? hour = 0;
  int? minutes = 0;

  getTaskByTime() async {
    isLoading = true;
    map = await TaskPageService.getTaskByTime(_selectedDay);
    isLoading = false;
    setState(() {});
  }

  getTaskTotalTime() async {
    totalTimeMap = await TaskPageService.getTaskTotalTime();
    print(totalTimeMap);
    TaskTimeEntity taskTimeEntity = TaskTimeEntity.fromJson(totalTimeMap);
    hour = taskTimeEntity.hours;
    minutes = taskTimeEntity.minutes;
  }

  Future<void> refresh() async {
    isLoading = true;
    setState(() {});
    map = await TaskPageService.getTaskByTime(_selectedDay);
    isLoading = false;
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    getTaskTotalTime();
    getTaskByTime();
  }

  @override
  Widget build(BuildContext context) {
    return ProgressWidget(
      loading: isLoading,
      msg: S.of(context).isLoading,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            padding: EdgeInsets.only(
              top: SizeConfig.getProportionateScreenHeight(18.0),
            ),
            width: SizeConfig.getProportionateScreenWidth(327.0),
            child: Column(
              children: [
                Padding(
                  padding: EdgeInsets.only(
                      top: SizeConfig.getProportionateScreenHeight(30.0)),
                  child: TableCalendar(
                    daysOfWeekHeight:
                        SizeConfig.getProportionateScreenHeight(25.0),
                    rowHeight: SizeConfig.getProportionateScreenHeight(45.0),
                    focusedDay: _focusedDay!,
                    firstDay: _firstDay,
                    headerStyle: const HeaderStyle(
                      titleCentered: true,
                      leftChevronVisible: true,
                      rightChevronVisible: true,
                      formatButtonVisible: false,
                    ),
                    lastDay: DateTime(
                        _firstDay.year, _firstDay.month + 1, _firstDay.day),
                    calendarFormat: _calendarFormat,
                    selectedDayPredicate: (day) {
                      return isSameDay(_selectedDay, day);
                    },
                    onFormatChanged: (format) {
                      setState(() {
                        _calendarFormat = format;
                      });
                    },
                    onDaySelected: (selectedDay, focusedDay) async {
                      _selectedDay = selectedDay;
                      _focusedDay = focusedDay;
                      getTaskByTime();
                      setState(() {});
                    },
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      S.of(context).today,
                      style: TextStyle(
                        fontSize: SizeConfig.getProportionateFontSize(24.0),
                      ),
                    ),
                    Text(
                      "$hour ${S.of(context).hour} $minutes ${S.of(context).minute}",
                      style: TextStyle(
                        fontSize: SizeConfig.getProportionateFontSize(15.0),
                      ),
                    ),
                  ],
                ),
                Column(
                  children: [
                    SizedBox(
                      height: SizeConfig.getProportionateScreenHeight(520.0),
                      child: RefreshIndicator(
                        onRefresh: refresh,
                        child: SingleChildScrollView(
                          physics: const AlwaysScrollableScrollPhysics(),
                          scrollDirection: Axis.vertical,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: TaskPageService.generateTaskList(
                                map, taskList, _selectedDay),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
